<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');




//check session see if user logged in
if(!function_exists('LoginSessionCheck')){
     function LoginSessionCheck(){
	  	 return SessionUser()!='' ;
     }
}

//set login session
if(!function_exists('SetLoginSession')){
     function SetLoginSession($uid){
     	  $CI = & get_instance();	
     	  $CI->session->set_userdata(SYS_USERID, $uid);
    }
}

//check cookie see if user  logged in
if (!function_exists('LoginCookieCheck')){
	function LoginCookieCheck(){
		   return CookieUser()!='';
	}	
}

//check cookie see if user  logged in
if (!function_exists('SetLoginCookie')){
	function SetLoginCookie($uid){
		$CI = & get_instance();	
		$CI -> load -> helper('cookie');   //load cookie helper
		set_cookie(SYS_USERID,$uid);
	}	
}


//set userid into session and cookie if needed
//by default, don't save user id into cookie
if (!function_exists('SetLoginInfo')){
	function SetLoginInfo($uid, $set_cookie=false){
		SetLoginSession($uid);
		if ($set_cookie) 
		   SetLoginCookie($uid);
	}
}

//erase user id information from session and cookie
//by default, don't reset cookie info
if (!function_exists('UnsetLoginInfo')){
	function UnsetLoginInfo($unset_session=true, $unset_cookie = false){
		$CI = & get_instance();
		if ($unset_session){  //unset session
			$CI -> session->unset_userdata(SYS_USERID);
		}
		if  ($unset_cookie){   //unset cookie
			$CI -> load->helper('cookie');
			delete_cookie(SYS_USERID);				
		}
	}
}

//check if login information valid
if (!function_exists('CheckLoginInfo')){
   function CheckLoginInfo(){
   	    if (!LoginSessionCheck()){   //session is not avaliable
   	       	return LoginCookieCheck();    //Check if cookie available
   	    }
		else {
			return true;
		}
   }


// get current user  id in session
if (!function_exists('SessionUser')){
	function SessionUser(){
		$CI = & get_instance();
 	    $sid = $CI->session->userdata(SYS_USERID);
		if (isset($sid)){  //check if we have session value
		  	 return trim($sid);
		}
		else {
			return '';
		}		
	}
}


// get current user  id in cookie
if (!function_exists('CookieUser')){
	function CookieUser(){
		$CI = & get_instance();
		$CI ->load->helper('cookie');
 	    $cid = get_cookie(SYS_USERID);
		if (isset($cid)){  //check if we have session value
		  	 return trim($cid);
		}
		else {
			return '';
		}
	}
}

// get current user id information
if (!function_exists('CurrentUser')){
   function CurrentUser(){
   	   $uid = SessionUser();  //read out user id from session
	   if ($uid==''){  //session userid is not available, check cookie
	   	  return CookieUser();
	   }
	   else {
	   	  return $uid;
	   }
   }	
}

	
}


